<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/1/16
 * Time: 10:45
 */

namespace app\controller\phone;
use app\model\phone\Keyword AS model;
use think\Exception;

class Index extends Base
{
    public function index(){
        $keyword = input('keyword','');
        if(!empty($keyword)){
            try {
                $keywords = model::where('keyword', $keyword)->find();
                if (empty($keywords->keyword)) {
                    $keywords = new \app\service\Keyword();
                    $string  = $keywords->save($keyword);
                    if ($string == 'NO')
                        throw new Exception('');
                    $keywords = model::where('keyword', $keyword)->find();
                }
            }catch (\Exception $e){
                $keywords = [
                    'keyword' => $keyword,
                    'all' => 0,
                    'price' => 0,
                    'pc' => 0,
                    'pc_price' => 0,
                    'phone' => 0,
                    'phone_price' => 0,
                    'keywords' => []
                ];
            }
        }
        $findAll = model::field(model::getfield())
            ->where('price','>',0)
            ->order('create_time DESC')
            ->limit(10)
            ->select();
        return $this->view('/phone/index',[
            'keyword' => $keyword,
            'list' => $findAll,
            'keywords' => $keywords ?? []
        ]);
    }
}